<cfcomponent output="false">

	<!--- --->

	<cffunction name="getDateRangesByDate" access="public" output="false" returntype="array">
		<cfargument name="date" required="false" default="#$.date.getDate()#" />
		<cfargument name="past" required="false" default="16" />
		<cfargument name="future" required="false" default="16" />
		
		<cfset var local = {} />
		
		<cfset local.result = [] />
		<cfset arrayAppend(local.result,arguments.date) />
		<!--- add future dates --->
		<cfloop from="1" to="#arguments.future#" index="local.i">
			<cfset local.date = $.format.date(dateAdd("d",local.i,arguments.date),"mm/dd/yyyy",false) />
			<cfset arrayAppend(local.result,local.date) />
		</cfloop>
		<cfloop from="1" to="#arguments.past#" index="local.i">
			<cfset local.date = $.format.date(dateAdd("d",local.i*-1,arguments.date),"mm/dd/yyyy",false) />
			<cfset arrayPrepend(local.result,local.date) />
		</cfloop>
		
		<cfreturn local.result />
		
	</cffunction>

	<!--- --->

	<cffunction name="getEvents" access="public" output="false" returntype="query">
		<cfargument name="startdate" required="false" default="" hint="localized" />
		<cfargument name="enddate" required="false" default="" hint="localized" />
		<cfargument name="userID" required="false" default="#request.session.user.id#" />
		
		<cfif len(arguments.startdate) eq 10>
			<cfset arguments.startdate = arguments.startdate & ' 00:00:00' />
		</cfif>
		
		<cfif len(arguments.enddate) eq 10>
			<cfset arguments.enddate = arguments.enddate & ' 23:59:59' />
		</cfif>
		
		<!--- to utc --->
		<cfif arguments.startdate neq "">
			<cfset arguments.startdate = $.date.localToUTC(arguments.startdate,request.session) />
		</cfif>
		<cfif arguments.enddate neq "">
			<cfset arguments.enddate = $.date.localToUTC(arguments.enddate,request.session) />
		</cfif>
				
		<cfreturn application.g.calendarGateway.getEvents(argumentCollection=arguments) />
	
	</cffunction>

	<!--- --->

	<cffunction name="saveEvent" access="public" output="false" returntype="string">
		<cfargument name="ID" required="true" />
		<cfargument name="description" required="true" />
		<cfargument name="start" required="true" hint="HH:mm:ss" />
		<cfargument name="stop" required="true" hint="HH:mm:ss"/>
		<cfargument name="category" required="false" default="0" />
		<cfargument name="date" required="true" hint="mm/dd/yyyy" />
		<cfargument name="google_calendar_id" required="false" default="" />
		<cfargument name="google_event_id" required="false" default="" />
		<cfargument name="google_calendar_link" required="false" default="" />
		<cfargument name="ignore_google" required="false" default="false" />
		<cfargument name="userID" required="false" default="#request.session.user.id#" />
		<cfargument name="parentID" required="false" default="" />
		<cfargument name="isLocalTime" required="false" default="true" />
		<cfargument name="categories" required="false" default="" />
		
		<cfset var local = {} />

		<cfset local.options = $.run.it(method='optionService.getOptions',args={key="calendar"}) />
		
		<cfif arguments.isLocalTime>
			<cfset local.start = $.date.localToUTC(arguments.date & " " & arguments.start) />
			<cfset local.stop = $.date.localToUTC(arguments.date & " " & arguments.stop) />
		<cfelse>
			<cfset local.start = arguments.start />
			<cfset local.stop = arguments.stop />
		</cfif>

		<cfif arguments.category eq "">
			<cfset arguments.category = 0 />
		</cfif>
		
		<cfset local.args = {
			id=arguments.ID,
			table="event",
			user_ID=arguments.userID,
			description=arguments.description,
			start=local.start,
			stop=local.stop,
			category=arguments.category,
			google_calendar_id=arguments.google_calendar_id,
			google_event_id=arguments.google_event_id,
			google_calendar_link=arguments.google_calendar_link
		} />
		
		<cfif arguments.parentID neq "">
			<cfset local.args.parent_id = arguments.parentID />
		</cfif>
		
		<cfset local.id = $.database.save(argumentCollection=local.args) />

		<!--- push or update to google --->
		<!--- child events are never seen by google --->
		<cfif arguments.ignore_google>
			<cfreturn local.id />
		</cfif>
		
		<cfif arguments.google_event_id neq "" and arguments.id neq ""> 
			<cfset $.run.it(method='googleService.updateGoogleEvent',args={
				googleEventID=arguments.google_event_id,
				googleCalendarID=arguments.google_calendar_id,
				description=arguments.description,
				starttime=local.start,
				stoptime=local.stop,
				category=arguments.category
			}) />
		<cfelse>
			<!--- push new event to google --->
			<!--- no save as new google event allowed for non-craftivity google events --->
			<cfif local.options.craftivity_calendar_id neq "">
				<cfset $.run.it(method='googleService.insertGoogleEvent',args={
					googleCalendarID=local.options.craftivity_calendar_id,
					description=arguments.description,
					starttime=local.start,
					stoptime=local.stop,
					category=arguments.category,
					eventID=local.id
				}) />	
			</cfif>
		</cfif>

		<cfreturn local.id />
		
	</cffunction>

	<!--- --->
	
	<cffunction name="deleteEvent" access="public" output="false" returntype="string">
		<cfargument name="eventID" required="true" />
		<cfargument name="google_event_id" required="false" default="" />
		<cfargument name="google_calendar_id" required="false" default="" />
		
		<cfset var local = {} />
		
		<cfset $.database.delete(
			id=arguments.eventID,
			table="event"
		) />
		
		<cfif arguments.google_event_id neq ''>
			<cfset local.result = $.run.it('googleAPIService.deleteEvent',{eventID=arguments.google_event_id,calendarID=arguments.google_calendar_id}) />
		</cfif>

		<cfreturn true />
		
	</cffunction>

	<!--- --->

	<cffunction name="saveEventCategories" access="public" output="false" returntype="string">
		<cfargument name="categories" required="true" />
		
		<cfset var local = {} />
		
		<cfset local.id = $.database.save(
			id=request.session.user.id,
			table="user",
			event_categories=arguments.categories
		) />
		
		<!--- invalidate cache on this user --->
		<cfset local.user = $.run.it('craftivityService.getUser',{userID=request.session.user.id,invalidateCache=true}) />
		
		<!--- update request with user values --->
		<cfset application.session.pushUserToSession(local.user) />
		
		<cfreturn local.id />
		
	</cffunction>

	<!--- --->

	<cffunction name="ajaxGetMonthEvents" access="public" output="false" returntype="array">
		<cfargument name="month" required="true" />
		<cfargument name="year" required="true" />
		
		<cfset var local = {} />
		
		<cfset local.startDate = arguments.month & '/01/' & arguments.year />
		<cfset local.stopDate = arguments.month & '/' & daysInMonth(local.startDate) & '/' & arguments.year />
		<cfset local.events = $.run.it('calendarService.getEvents', {startdate=local.startDate,enddate=local.stopDate}) />
		
		<cfset local.localizedEvents = [] />
		<!--- local.localizedEvents is passed to Kalendar.loadMonth() --->
		<cfloop query="local.events">
			<!--- see leftView's Kalendar.addEvent for reference to how to setup the array/struct (line 250)--->
			<!--- localize the UTC datetime --->
			<cfset local.event_start = $.date.applyLocalization(local.events.start_date & ' #local.events.start_time#') />
			<cfset local.event_stop = $.date.applyLocalization(local.events.start_date & ' #local.events.stop_time#') />
			<!--- format for renderEvent() --->
			<cfset local.event_date = dateFormat(local.event_start,'mm/dd/yyyy') />
			<cfset local.event_starttime = timeFormat(local.event_start,'HH:mm:ss') />
			<cfset local.event_stoptime = timeFormat(local.event_stop,'HH:mm:ss') />
			
			<cfset local.event = {
				date=local.event_date,
				starttime=local.event_starttime,
				stoptime=local.event_stoptime,
				description=rereplace($.format.paragraph(local.events.description,false,true),"'","\'"),
				category=rereplace(local.events.category,"'","\'"),
				id=local.events.id,
				google_calendar_link=local.events.google_calendar_link,
				google_event_id=local.events.google_event_id,
				google_calendar_id=local.events.google_calendar_id
			} />
			<cfset arrayAppend(local.localizedEvents,local.event) />
		</cfloop>
		
		<cfreturn local.localizedEvents />
		
	</cffunction>

	<!--- --->
	
	<cffunction name="updateEventsByTimezoneChange" access="public" output="false" returntype="void">
		<cfargument name="before" required="true" hint="-6:00" />
		<cfargument name="after" required="true" hint="-6:00" />
		
		<!--- disabled for now --->
		
		<cfset var local = {} />
		
		<cfreturn />
		
		<cfset local.delta = listGetAt(arguments.before,1,":") - listGetAt(arguments.after,1,":") />
		
		<!--- update all events (except google events) according to timezone change so no change is perceived by user --->
		<cfif local.delta neq 0>
			<cfquery name="local.updateevents" datasource="#$.site.getVar('datasource')#">
				update event 
				set start = date_add(start,INTERVAL #local.delta# hour),
				stop = date_add(stop,INTERVAL #local.delta# hour)
				where active = 1
				and google_event_id is null
				and event.user_id = #$.database.uuid(request.session.user.id)#
			</cfquery>
		</cfif>
		
		<cfreturn />
		
	</cffunction>

	<!--- --->
	
	<cffunction name="saveFilters" access="public" output="true" returntype="void">
		<cfargument name="days" required="false" default="" />
		<cfargument name="categories" false="false" default="" />
		
		<cfset var local = {} />
		
		<cfset $.run.it('optionService.saveOptions',{
			key="calendar",
			args={filterDays=arguments.days,filterCategories=arguments.categories}
		}) />

	</cffunction>
	
	<!--- --->

	<cffunction name="getSecretaryEvents" access="public" output="false" returntype="query">

		<cfreturn application.g.calendarGateway.getSecretaryEvents(argumentCollection=arguments) />

	</cffunction>

	<!--- --->
	
	<cffunction name="getGoogleThreadStatus" access="public" output="false" returntype="string">
		
		<cfset var local = {} />
		
		<cfset local.options = $.site.getOptions('calendar') />
		
		<cfset local.pullThreadOutcome = "ERROR" />
		<cfset local.pushThreadOutcome = "ERROR" />
		
		<cfif local.options.pullThread eq ''><cfset local.pullThreadOutcome = 'COMPLETED'></cfif>
		<cfif local.options.pushThread eq ''><cfset local.pushThreadOutcome = 'COMPLETED'></cfif>
		
		<cfloop array="#application.threads#" item="local.item" index="local.i">
			<cfif local.item.name eq local.options.pullThread>
				<cfif local.item.status eq 'RUNNING'>
					<cfset local.pullThreadOutcome = 'RUNNING' />
				<cfelseif local.item.status eq 'COMPLETED'>
					<cfset local.pullThreadOutcome = 'COMPLETED' />
				</cfif>
			</cfif>
			<cfif local.item.name eq local.options.pushThread>
				<cfif local.item.status eq 'RUNNING'>
					<cfset local.pushTHreadOutcome = 'RUNNING' />
				<cfelseif local.item.status eq 'COMPLETED'>
					<cfset local.pushTHreadOutcome = 'COMPLETED' />
				</cfif>
			</cfif>
		</cfloop>
		
		<cfset local.threadStatus = "" />
		
		<cfif local.pushThreadOutcome eq 'RUNNING' or local.pullThreadOutcome eq "RUNNING">
			<cfset local.threadStatus = "RUNNING" />
		<cfelseif local.pushThreadOutcome eq 'ERROR' or local.pullThreadOutcome eq 'ERROR'>
			<cfset local.threadStatus = "ERROR" />
		<cfelse>
			<cfset local.threadStatus = "" />
		</cfif>
		
		<cfreturn local.threadStatus />

	</cffunction>

	<!--- --->

</cfcomponent>
